/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using System.Qizx.XQuery;
namespace System.Qizx.XQuery.Op
{
	
	/// <summary> Implementation of operator 'cast as'.</summary>
	public class CastExpr:Expression
	{
		
		public Expression expr;
		
		public XQItemType targetType;
		
		protected internal bool isOptional;
		
		public CastExpr(Expression expr, XQType targetType)
		{
			this.expr = expr;
			this.targetType = targetType.itemType();
			isOptional = targetType is SequenceType; // only
			// (ItemType.opt)
		}
		
		public override Expression Child(int rank)
		{
			return (rank == 0)?expr:null;
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.property("targetType", targetType);
			d.child("expr", expr);
		}
		
		public override Expression StaticCheck(ModuleContext context, int flags)
		{
			expr = context.staticCheck(expr, 0);
			if (targetType == XQType.ITEM || targetType == XQType.ATOM || targetType == XQType.NOTATION)
				context.error("XPST0080", this, "invalid cast target type");
			type = targetType;
			return this;
		}
		
		public override XQValue eval(Focus focus, EvalContext context)
		{
			XQValue v = expr.eval(focus, context), result;
			if (!v.Next())
			{
				if (!isOptional)
				{
					context.error("XPTY0004", this, "empty sequence cannot be cast to type " + targetType);
				}
				else
					return System.Qizx.XQuery.XQValues.Empty;
			}
			try
			{
				result = targetType.cast(v, context);
			}
			catch (XQTypeException err)
			{
				return context.error(this, err);
			}
			if (v.Next())
				context.error("XPTY0004", this, "singleton expected in cast");
			return result;
		}
	}
}